<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="license">
        <el-input
          v-model="queryParams.license"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机工号" prop="driverNum">
        <el-input
          v-model="queryParams.driverNum"
          placeholder="请输入司机工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出发日期" prop="departureDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.departureDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择出发日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出发地" prop="departure">
        <el-input
          v-model="queryParams.departure"
          placeholder="请输入出发地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目的地" prop="destination">
        <el-input
          v-model="queryParams.destination"
          placeholder="请输入目的地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上架/下架" prop="online">
        <el-select v-model="queryParams.online" placeholder="请选择上架/下架" clearable size="small">
          <el-option
            v-for="dict in onlineOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否删除" prop="deleted">
        <el-select v-model="queryParams.deleted" placeholder="请选择是否删除" clearable size="small">
          <el-option
            v-for="dict in deletedOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="员工座位" prop="sSeat">
        <el-input
          v-model="queryParams.sSeat"
          placeholder="请输入员工座位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="普通座位" prop="nSeat">
        <el-input
          v-model="queryParams.nSeat"
          placeholder="请输入普通座位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工剩余" prop="sAllow">
        <el-input
          v-model="queryParams.sAllow"
          placeholder="请输入员工剩余"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="普通剩余" prop="nAllow">
        <el-input
          v-model="queryParams.nAllow"
          placeholder="请输入普通剩余"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工票价" prop="sPrice">
        <el-input
          v-model="queryParams.sPrice"
          placeholder="请输入员工票价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="普通票价" prop="nPrice">
        <el-input
          v-model="queryParams.nPrice"
          placeholder="请输入普通票价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['departureManage:departure:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['departureManage:departure:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['departureManage:departure:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['departureManage:departure:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="departureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车次id" align="center" prop="id" />
      <el-table-column label="车牌号" align="center" prop="license" />
      <el-table-column label="司机工号" align="center" prop="driverNum" />
      <el-table-column label="出发日期" align="center" prop="departureDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departureDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出发地" align="center" prop="departure" />
      <el-table-column label="目的地" align="center" prop="destination" />
      <el-table-column label="上架/下架" align="center" prop="online" :formatter="onlineFormat" />
      <el-table-column label="是否删除" align="center" prop="deleted" :formatter="deletedFormat" />
      <el-table-column label="员工座位" align="center" prop="sSeat" />
      <el-table-column label="普通座位" align="center" prop="nSeat" />
      <el-table-column label="员工剩余" align="center" prop="sAllow" />
      <el-table-column label="普通剩余" align="center" prop="nAllow" />
      <el-table-column label="员工票价" align="center" prop="sPrice" />
      <el-table-column label="普通票价" align="center" prop="nPrice" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['departureManage:departure:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['departureManage:departure:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车次对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车牌号" prop="license">
          <el-input v-model="form.license" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="司机工号" prop="driverNum">
          <el-input v-model="form.driverNum" placeholder="请输入司机工号" />
        </el-form-item>
        <el-form-item label="出发日期" prop="departureDate">
          <el-date-picker clearable size="small"
            v-model="form.departureDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择出发日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出发地" prop="departure">
          <el-input v-model="form.departure" placeholder="请输入出发地" />
        </el-form-item>
        <el-form-item label="目的地" prop="destination">
          <el-input v-model="form.destination" placeholder="请输入目的地" />
        </el-form-item>
        <el-form-item label="上架/下架" prop="online">
          <el-select v-model="form.online" placeholder="请选择上架/下架">
            <el-option
              v-for="dict in onlineOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否删除" prop="deleted">
          <el-select v-model="form.deleted" placeholder="请选择是否删除">
            <el-option
              v-for="dict in deletedOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工座位" prop="sSeat">
          <el-input v-model="form.sSeat" placeholder="请输入员工座位" />
        </el-form-item>
        <el-form-item label="普通座位" prop="nSeat">
          <el-input v-model="form.nSeat" placeholder="请输入普通座位" />
        </el-form-item>
        <el-form-item label="员工剩余" prop="sAllow">
          <el-input v-model="form.sAllow" placeholder="请输入员工剩余" />
        </el-form-item>
        <el-form-item label="普通剩余" prop="nAllow">
          <el-input v-model="form.nAllow" placeholder="请输入普通剩余" />
        </el-form-item>
        <el-form-item label="员工票价" prop="sPrice">
          <el-input v-model="form.sPrice" placeholder="请输入员工票价" />
        </el-form-item>
        <el-form-item label="普通票价" prop="nPrice">
          <el-input v-model="form.nPrice" placeholder="请输入普通票价" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeparture, getDeparture, delDeparture, addDeparture, updateDeparture, exportDeparture } from "@/api/departureManage/departure";

export default {
  name: "Departure",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车次表格数据
      departureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 上架/下架字典
      onlineOptions: [],
      // 是否删除字典
      deletedOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        license: null,
        driverNum: null,
        departureDate: null,
        departure: null,
        destination: null,
        online: null,
        deleted: null,
        sSeat: null,
        nSeat: null,
        sAllow: null,
        nAllow: null,
        sPrice: null,
        nPrice: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        license: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        driverNum: [
          { required: true, message: "司机工号不能为空", trigger: "blur" }
        ],
        departureDate: [
          { required: true, message: "出发日期不能为空", trigger: "blur" }
        ],
        departure: [
          { required: true, message: "出发地不能为空", trigger: "blur" }
        ],
        destination: [
          { required: true, message: "目的地不能为空", trigger: "blur" }
        ],
        online: [
          { required: true, message: "上架/下架不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        deleted: [
          { required: true, message: "是否删除不能为空", trigger: "change" }
        ],
        sSeat: [
          { required: true, message: "员工座位不能为空", trigger: "blur" }
        ],
        nSeat: [
          { required: true, message: "普通座位不能为空", trigger: "blur" }
        ],
        sAllow: [
          { required: true, message: "员工剩余不能为空", trigger: "blur" }
        ],
        nAllow: [
          { required: true, message: "普通剩余不能为空", trigger: "blur" }
        ],
        sPrice: [
          { required: true, message: "员工票价不能为空", trigger: "blur" }
        ],
        nPrice: [
          { required: true, message: "普通票价不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("is_online").then(response => {
      this.onlineOptions = response.data;
    });
    this.getDicts("is_deleted").then(response => {
      this.deletedOptions = response.data;
    });
  },
  methods: {
    /** 查询车次列表 */
    getList() {
      this.loading = true;
      listDeparture(this.queryParams).then(response => {
        this.departureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 上架/下架字典翻译
    onlineFormat(row, column) {
      return this.selectDictLabel(this.onlineOptions, row.online);
    },
    // 是否删除字典翻译
    deletedFormat(row, column) {
      return this.selectDictLabel(this.deletedOptions, row.deleted);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        license: null,
        driverNum: null,
        departureDate: null,
        departure: null,
        destination: null,
        online: null,
        createTime: null,
        updateTime: null,
        deleted: null,
        sSeat: null,
        nSeat: null,
        sAllow: null,
        nAllow: null,
        sPrice: null,
        nPrice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车次";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDeparture(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车次";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeparture(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeparture(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除车次编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDeparture(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有车次数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDeparture(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    }
  }
};
</script>
